﻿using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using RFS.BizLogic.PaiShi;
using RFS.BizLogic.PaiShi.Accounting;
using RFS.BizLogic.PaiShi.Impl;
using RFS.BizLogic.PaiShi.Ports;
using RFS.BizLogic.PaiShi.Ports.Adapters;
using RFS.BizLogic.Ports;
using RFS.BizLogic.Ports.Adapters;
using RFS.BizLogic.utils;
using RFS.BizLogic.utils.impl;
using RFS.Domain.PaiShi;
using RFS.Domain.Sys;
// using RFS.WPF.Views;

namespace RFS.BizLogic.Sys.impl
{
    public class AntiObject : IAntiObject
    {
        public AntiObject()
        {
            

        }


        public static IAntiObject Ins
        {
            get
            {
                if (_instance==null) BuildMapper();
                return _instance;
            }
        }


        static  IAntiObject _instance;
        static ServiceCollection svs=new ServiceCollection();

        static void BuildMapper()
        {
            regProd(svs );
            _ServiceProvider= svs.BuildServiceProvider();
            _instance=_ServiceProvider.GetService<IAntiObject>();
        }
       static void regProd(ServiceCollection svsa){
           // var svs=new ServiceCollection();
            svsa.AddSingleton<ILog, Log>();
            svsa.AddSingleton<IAntiObject, AntiObject>();
            svsa.AddSingleton<Sys, Sys>();
            // svsa.AddSingleton<SysVm, SysVm >();
            svsa.AddSingleton<ISys, Sys>();
            svsa.AddSingleton<IConfigT, ConfigMySqlAdapter>();
            // svsa.AddSingleton<MainWindow, MainWindow>();
            svsa.AddSingleton< IBlobRepo, BlobAliOssRepo > ();
            svsa.AddSingleton< IRepoPaiShi, RepoProductsMySqlAdapter > ();
            // svsa.AddSingleton< IJiaoYiDayBasedBatchManager, JiaoYiDayBasedBatchManager > ();
            
            svsa.AddSingleton< IWorkFlowManager, WorkFlowManager > ();
            svsa.AddSingleton< IPaiShiOrg,  PaiShiOrgOrg > ();
            svsa.AddSingleton< IPriceManagerPort, PriceManagerJiaoYiRecordsBasedAdapter  > ();
            
            svsa.AddSingleton< IAccountingVoucherManager, AccountingVoucherManager> ();
            svsa.AddSingleton< IAVSRepoPort, AvsRepoMysqlAdapter> ();
            svsa.AddSingleton< IRepoAcc, AccRepoMySqlAdapter > ();
  
        }
        
        

       static IServiceProvider _ServiceProvider;
        public T Get<T>()
        {
            return  _ServiceProvider.GetService<T>(  );
        }

        public void RegisterMock4Test<T>(T jiaoYiDayBasedBatchManager)  
        {
           
            svs.Replace(ServiceDescriptor.Scoped ( typeof(T)  ,  (x)=>jiaoYiDayBasedBatchManager) );
                        
            _ServiceProvider= svs.BuildServiceProvider();
            _instance=_ServiceProvider.GetService<IAntiObject>();
            
            
            
        }

        public void RegisterService(Action<ServiceCollection> act)
        {
            act.Invoke(svs);
            _ServiceProvider= svs.BuildServiceProvider();
            _instance=_ServiceProvider.GetService<IAntiObject>();

        }

        public static IAntiObject OfTestSandbox()
        {

            Ins.RegisterMock4Test<IRepoPaiShi>(new RepoTestData());
            return Ins;
            
        }
    }
}